package com.henu.service;

import java.util.List;
import java.util.Map;

import org.smart4j.framework.dao.DatabaseHelper;
import org.smart4j.framework.tx.annotation.Service;
import org.smart4j.framework.util.CollectionUtil;

import com.henu.util.Encript;

@Service
public class UserLoginService {

    /**
     * 验证用户密码是否存在
     * @param userName
     * @param password
     * @return 0：用户名或者密码错误，1：验证通过
     */
    public int checkUserLogin(String userName, String password) {
        Map userBean = queryUserByUsername(userName);
        if(userBean==null) return 0;
        String password1 = (String) userBean.get("password");
        if (Encript.authenticatePassword(password, password1)) {
            return (Integer) userBean.get("id");
        }
        return 0;
    }

    /**
     * 根据用户名查询用户信息
     * @param userName
     * @return
     */
    public Map queryUserByUsername(String userName) {
        List<Map<String, Object>> userBean = DatabaseHelper.queryMapList(
                "select * from userlogin t where t.userName =?", userName);
        if (CollectionUtil.isEmpty(userBean)) {
            return null;
        }
        return userBean.get(0);
    }

    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     */
    public Map queryUserById(int userId) {
        List<Map<String, Object>> userBean = DatabaseHelper.queryMapList(
                "select * from userlogin t where t.id =?", userId);
        if (CollectionUtil.isEmpty(userBean)) {
            return null;
        }
        return userBean.get(0);
    }
}
